package com.yuncheng.spcyApi.controller;

import java.lang.reflect.InvocationTargetException;
import java.util.*;
import javax.annotation.Resource;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.spcyApi.alog.annotation.Log;
import com.yuncheng.spcyApi.constant.common.LogConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.mapper.SpcyQyCpxxMapper;
import com.yuncheng.spcyApi.service.ISpcyLogService;
import com.yuncheng.spcyApi.utils.DateConvert;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.vo.HttpResult;
import com.yuncheng.common.base.controller.BaseController;
import com.yuncheng.spcyApi.entity.SpcyQyCpxx;
import com.yuncheng.spcyApi.service.ISpcyQyCpxxService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yuncheng.spcyApi.vo.qy.QyCpVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import utils.LogUtils;
import utils.ObjectToMapUtils;

/**
 * @Description: 审评查验-企业产品信息
 * @Author: yuncheng
 * @Date: 2023-03-17
 * @Version: V1.0
 */
@Api(tags = "审评查验-企业产品信息接口")
@RestController
@RequestMapping("/api/spzx-spcy/spcyQyCpxx")
public class SpcyQyCpxxController extends BaseController<SpcyQyCpxx, ISpcyQyCpxxService> {

    private static final Logger log = LoggerFactory.getLogger(SpcyQyCpxxController.class);

    @Resource
    private ISpcyQyCpxxService spcyQyCpxxService;

    @Resource
    private SpcyQyCpxxMapper spcyQyCpxxMapper;

    @Resource
    private ISpcyLogService spcyLogService;


    @Log(title = "获取企业的所有产品")
    @ApiOperation(value = "获取企业的所有产品")
    @GetMapping(value = "queryAllQyCpList")
    public HttpResult<?> queryAllQyCpList(QyCpVo qyCpVo){
        List<QyCpVo> cpList = spcyQyCpxxMapper.getCpList(qyCpVo);
        return HttpResult.ok(cpList);
    }


    @Log(title = "分页获取-产品信息")
    @ApiOperation(value = "分页获取-产品信息")
    @GetMapping(value = "queryPageList")
    public HttpResult<?> queryPageList(QyCpVo qyCpVo,@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                       @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize){
        Page<QyCpVo> page = new Page<>(pageNo,pageSize);
        IPage<QyCpVo> cpPageList = spcyQyCpxxMapper.getCpPageList(page, qyCpVo);
        return HttpResult.ok(cpPageList);
    }


    @Log(title = "企业产品信息-添加")
    @ApiOperation(value = "企业产品信息-添加")
    @PostMapping(value = "/addCpxx")
    public HttpResult<?> addCpxx(@RequestBody SpcyQyCpxx spcyQyCpxx) {
        if (StringUtils.isBlank(spcyQyCpxx.getFqyid())){
            return HttpResult.error("企业id不能为空");
        }
        if (StringUtils.isBlank(spcyQyCpxx.getFcpmc())){
            return HttpResult.error("产品名称不能为空");
        }
        List<SpcyQyCpxx> cpList = spcyQyCpxxService.list(new LambdaQueryWrapper<SpcyQyCpxx>()
                .eq(SpcyQyCpxx::getFqyid, spcyQyCpxx.getFqyid())
                .eq(SpcyQyCpxx::getFcpmc, spcyQyCpxx.getFcpmc()));
        if (CollectionUtils.isEmpty(cpList)) {

            spcyQyCpxx.setId(GetUuIdUtils.ReplaceUuId());
            spcyQyCpxxService.save(spcyQyCpxx);

            String operContent = "新增信息：";
            // 将map里面的属性和值转String
            Map<String, Object> beanMap = ObjectToMapUtils.objectToMap(spcyQyCpxx);
            operContent += LogUtils.MapToStringMethod(beanMap);
            spcyLogService.addLogs(LogConstant.LOGS_QY_CPXX, operContent, spcyQyCpxx.getId(), LogConstant.LOGS_QY_CPXX, SpcyConstant.LOGS_SYS_TYPE_XZ);
            return HttpResult.ok("添加成功");
        }else {
            return HttpResult.error("当前企业已经存在该产品【"+spcyQyCpxx.getFcpmc()+"】了");
        }
    }


    @Log(title = "企业产品信息-编辑")
    @ApiOperation(value = "企业产品信息-编辑")
    @PutMapping(value = "/editCpxx")
    public HttpResult<?> editCpxx(@RequestBody HashMap<String, String> buffer) throws InvocationTargetException, IllegalAccessException{
        if (StringUtils.isEmpty(buffer.get("id"))){
            return HttpResult.error("产品基本信息id不能为空");
        }
        String cpId = buffer.get("id");
        SpcyQyCpxx oldCpxx = spcyQyCpxxService.getById(cpId);
        SpcyQyCpxx cpxxUpdate = spcyQyCpxxService.getById(cpId);
        if (cpxxUpdate == null){
            return HttpResult.error("当前产品基本信息数据不存在，请重试");
        }
        ConvertUtils.register(new DateConvert(), java.util.Date.class);
        // 自动完成页面到bean的数据对接
        BeanUtils.populate(cpxxUpdate, buffer);
        spcyQyCpxxService.updateById(cpxxUpdate);

        String operContent = "基本信息修改：";
        Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(oldCpxx);
        operContent += LogUtils.BeanModifyMsg(cpxxUpdate,oldMap);

        spcyLogService.addLogs(LogConstant.LOGS_QY_CPXX,operContent,cpId,LogConstant.LOGS_QY_CPXX,SpcyConstant.LOGS_SYS_TYPE_XG);
        return HttpResult.ok("修改成功");
    }


    @Log(title = "企业产品信息-批量删除")
    @ApiOperation(value = "企业产品信息-批量删除")
    @DeleteMapping(value = "/deleteBatchCpxx")
    public HttpResult<?> deleteBatchCpxx(@RequestParam("ids") String ids) {
        if (StringUtils.isEmpty(ids)){
            return HttpResult.error("ids参数不能为空");
        }
        List<String> idsList = Arrays.asList(ids.split(","));
        List<SpcyQyCpxx> cpxxList = spcyQyCpxxService.listByIds(idsList);
        if (CollectionUtils.isEmpty(cpxxList)){
            return HttpResult.error("数据不存在，请重试");
        }
        String operContent = "";
        for (SpcyQyCpxx cpxx : cpxxList){
            String id = cpxx.getId();
            spcyQyCpxxService.removeById(id);

            operContent = "删除信息：";
            Map<String, Object> oldMap = ObjectToMapUtils.objectToMap(cpxx);
            operContent += LogUtils.MapToStringMethod(oldMap);
            spcyLogService.addLogs(LogConstant.LOGS_QY_CPXX,operContent,id,LogConstant.LOGS_QY_CPXX,SpcyConstant.LOGS_SYS_TYPE_SC);
            operContent = "";
        }

        return HttpResult.ok("删除成功");
    }


    @Log(title = "企业产品信息-通过id查询")
    @ApiOperation(value = "企业产品信息-通过id查询")
    @GetMapping(value = "/queryById")
    public HttpResult<?> queryById(QyCpVo qyCpVo,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
//        Page<QyCpVo> page = new Page<>(pageNo,pageSize);
//        IPage<QyCpVo> cpPageList = spcyQyCpxxMapper.getCpPageList(page, qyCpVo);
        return HttpResult.ok();
    }

}
